home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume5 / pbm3 / part4 < prev    next >
Encoding:
Internet Message Format  |  1989-02-03  |  44.6 KB

  1. Path: xanth!nic.MR.NET!hal!ncoast!allbery
  2. From: jef@helios.ee.lbl.gov (Jef Poskanzer)
  3. Newsgroups: comp.sources.misc
  4. Subject: v05i044: portable bitmap routines (4 of 5)
  5. Message-ID: <8811010237.AA02700@helios.ee.lbl.gov>
  6. Date: 9 Nov 88 02:40:02 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: jef@helios.ee.lbl.gov (Jef Poskanzer)
  9. Lines: 1765
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. Posting-number: Volume 5, Issue 44
  13. Submitted-by: "Jef Poskanzer" <jef@helios.ee.lbl.gov>
  14. Archive-name: pbm3/Part4
  15.  
  16. #! /bin/sh
  17. # This is a shell archive, meaning:
  18. # 1. Remove everything above the #! /bin/sh line.
  19. # 2. Save the resulting text in a file.
  20. # 3. Execute the file with /bin/sh (not csh) to create the files:
  21. #    pbmtomacp.1
  22. #    pbmtox10wd.c
  23. #    pbmtox10wd.1
  24. #    pbmtoxwd.c
  25. #    pbmtoxwd.1
  26. #    brushtopbm.c
  27. #    brushtopbm.1
  28. #    libpbm1.c
  29. #    libpbm2.c
  30. #    libpbm3.c
  31. #    libpbm4.c
  32. #    libpbm5.c
  33. #    giftopbm.c
  34. #    giftopbm.1
  35. # This archive created: Mon Oct 31 18:33:38 1988
  36. # By:    Jef Poskanzer (Paratheo-Anametamystikhood Of Eris Esoteric, Ada Lovelace Cabal)
  37. export PATH; PATH=/bin:$PATH
  38. echo shar: extracting "'pbmtomacp.1'" '(1091 characters)'
  39. if test -f 'pbmtomacp.1'
  40. then
  41.     echo shar: will not over-write existing file "'pbmtomacp.1'"
  42. else
  43. sed 's/^X//' << \SHAR_EOF > 'pbmtomacp.1'
  44. X.TH pbmtomacp 1 "31 August 1988"
  45. X.SH NAME
  46. Xpbmtomacp - convert portable bitmap into MacPaint
  47. X.SH SYNOPSIS
  48. Xpbmtomacp [-l left] [-r right] [-b bottom] [-t top] [pbmfile]
  49. X.SH DESCRIPTION
  50. XReads a portable bitmap as input.
  51. XIf no input-file is given, standard input is assumed.
  52. XProduces a MacPaint file as output.
  53. X.LP
  54. XLeft, right,bottom & top let you define a square into the pbm file,
  55. Xthat must be converted.
  56. XDefault is the whole file.
  57. XIf the file is too large for a MacPaint-file, the bitmap is cut to fit
  58. Xfrom ( left, top ).
  59. X.SH BUGS
  60. XThe source code contains comments in a language other than English.
  61. X.SH "SEE ALSO"
  62. Xmacptopbm(1), pbm(5)
  63. X.SH AUTHOR
  64. XCopyright (C) 1988 by Douwe van der Schaaf.
  65. XUSENET: ..!mcvax!uvapsy!vdschaaf
  66. X
  67. XPermission to use, copy, modify, and distribute this software and its
  68. Xdocumentation for any purpose and without fee is hereby granted, provided
  69. Xthat the above copyright notice appear in all copies and that both that
  70. Xcopyright notice and this permission notice appear in supporting
  71. Xdocumentation.  This software is provided "as is" without express or
  72. Ximplied warranty.
  73. SHAR_EOF
  74. if test 1091 -ne "`wc -c < 'pbmtomacp.1'`"
  75. then
  76.     echo shar: error transmitting "'pbmtomacp.1'" '(should have been 1091 characters)'
  77. fi
  78. fi # end of overwriting check
  79. echo shar: extracting "'pbmtox10wd.c'" '(2873 characters)'
  80. if test -f 'pbmtox10wd.c'
  81. then
  82.     echo shar: will not over-write existing file "'pbmtox10wd.c'"
  83. else
  84. sed 's/^X//' << \SHAR_EOF > 'pbmtox10wd.c'
  85. X/* pbmtox10wd.c - read a portable bitmap and produce an X10 window dump
  86. X**
  87. X** Copyright (C) 1988 by Jef Poskanzer.
  88. X**
  89. X** Permission to use, copy, modify, and distribute this software and its
  90. X** documentation for any purpose and without fee is hereby granted, provided
  91. X** that the above copyright notice appear in all copies and that both that
  92. X** copyright notice and this permission notice appear in supporting
  93. X** documentation.  This software is provided "as is" without express or
  94. X** implied warranty.
  95. X*/
  96. X
  97. X#include <stdio.h>
  98. X#ifdef    OS_SYSV
  99. X#include <string.h>
  100. X#else    OS_SYSV
  101. X#include <strings.h>
  102. X#endif    OS_SYSV
  103. X#include "pbm.h"
  104. X#include "x10wd.h"
  105. X
  106. Xmain( argc, argv )
  107. Xint argc;
  108. Xchar *argv[];
  109. X    {
  110. X    FILE *ifd;
  111. X    bit **bits;
  112. X    int rows, cols, padright, row, col;
  113. X    char name[100], *cp;
  114. X
  115. X    if ( argc > 2 )
  116. X    {
  117. X    fprintf( stderr, "usage:  %s [pbmfile]\n", argv[0] );
  118. X    exit( 1 );
  119. X    }
  120. X
  121. X    if ( argc == 2 )
  122. X    {
  123. X        ifd = fopen( argv[1], "r" );
  124. X        if ( ifd == NULL )
  125. X        {
  126. X        fprintf( stderr, "%s: can't open.\n", argv[1] );
  127. X        exit( 1 );
  128. X        }
  129. X    strcpy( name, argv[1] );
  130. X
  131. X#ifdef    OS_SYSV
  132. X    if ( ( cp = strchr( name, '.' ) ) != 0 )
  133. X#else    OS_SYSV
  134. X    if ( ( cp = index( name, '.' ) ) != 0 )
  135. X#endif    OS_SYSV
  136. X        *cp = '\0';
  137. X    }
  138. X    else
  139. X    {
  140. X    ifd = stdin;
  141. X    strcpy( name, "noname" );
  142. X    }
  143. X
  144. X    bits = pbm_readpbm( ifd, &cols, &rows );
  145. X
  146. X    if ( ifd != stdin )
  147. X    fclose( ifd );
  148. X    
  149. X    /* Compute padding to round cols up to the nearest multiple of 16. */
  150. X    padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  151. X
  152. X    putinit( cols, rows, name );
  153. X    for ( row = 0; row < rows; row++ )
  154. X    {
  155. X        for ( col = 0; col < cols; col++ )
  156. X        putbit( bits[row][col] );
  157. X    for ( col = 0; col < padright; col++ )
  158. X        putbit( 0 );
  159. X        }
  160. X    putrest( );
  161. X
  162. X    exit( 0 );
  163. X    }
  164. X
  165. X
  166. Xshort item;
  167. Xint bitsperitem, bitshift;
  168. X
  169. X
  170. Xputinit( cols, rows, name )
  171. Xint cols, rows;
  172. Xchar *name;
  173. X    {
  174. X    X10WDFileHeader h10;
  175. X
  176. X    h10.header_size = sizeof(h10) + (strlen(name) + 1) * sizeof(char);
  177. X    h10.file_version = X10WD_FILE_VERSION;
  178. X    h10.display_type = -1;        /* random number */
  179. X    h10.display_planes = 1;
  180. X    h10.pixmap_format = XYFormat;
  181. X    h10.pixmap_width = cols;
  182. X    h10.pixmap_height = rows;
  183. X    h10.window_width = cols;
  184. X    h10.window_height = rows;
  185. X    h10.window_x = 0;            /* arbitrary */
  186. X    h10.window_y = 0;            /* likewise */
  187. X    h10.window_bdrwidth = 0;
  188. X    h10.window_ncolors = 0;
  189. X
  190. X    fwrite( &h10, sizeof(h10), 1, stdout );
  191. X    fwrite( name, sizeof(char), strlen(name) + 1, stdout );
  192. X
  193. X    item = 0;
  194. X    bitsperitem = 0;
  195. X    bitshift = 0;
  196. X    }
  197. X
  198. Xputbit( b )
  199. Xbit b;
  200. X    {
  201. X    if ( bitsperitem == 16 )
  202. X    putitem( );
  203. X    bitsperitem++;
  204. X    if ( ! b )
  205. X    item += 1 << bitshift;
  206. X    bitshift++;
  207. X    }
  208. X
  209. Xputrest( )
  210. X    {
  211. X    if ( bitsperitem > 0 )
  212. X    putitem( );
  213. X    }
  214. X
  215. Xputitem( )
  216. X    {
  217. X    fwrite( &item, sizeof(item), 1, stdout );
  218. X    item = 0;
  219. X    bitsperitem = 0;
  220. X    bitshift = 0;
  221. X    }
  222. SHAR_EOF
  223. if test 2873 -ne "`wc -c < 'pbmtox10wd.c'`"
  224. then
  225.     echo shar: error transmitting "'pbmtox10wd.c'" '(should have been 2873 characters)'
  226. fi
  227. fi # end of overwriting check
  228. echo shar: extracting "'pbmtox10wd.1'" '(691 characters)'
  229. if test -f 'pbmtox10wd.1'
  230. then
  231.     echo shar: will not over-write existing file "'pbmtox10wd.1'"
  232. else
  233. sed 's/^X//' << \SHAR_EOF > 'pbmtox10wd.1'
  234. X.TH pbmtox10wd 1 "31 August 1988"
  235. X.SH NAME
  236. Xpbmtox10wd - convert portable bitmaps into X10 window dumps
  237. X.SH SYNOPSIS
  238. Xpbmtox10wd [pbmfile]
  239. X.SH DESCRIPTION
  240. XReads a portable bitmap as input.
  241. XProduces an X10 window dump as output.
  242. X.SH "SEE ALSO"
  243. Xpbmtoxwd(1), xwdtopbm(1), pbm(5)
  244. X.SH AUTHOR
  245. XCopyright (C) 1988 by Jef Poskanzer.
  246. X
  247. XPermission to use, copy, modify, and distribute this software and its
  248. Xdocumentation for any purpose and without fee is hereby granted, provided
  249. Xthat the above copyright notice appear in all copies and that both that
  250. Xcopyright notice and this permission notice appear in supporting
  251. Xdocumentation.  This software is provided "as is" without express or
  252. Ximplied warranty.
  253. SHAR_EOF
  254. if test 691 -ne "`wc -c < 'pbmtox10wd.1'`"
  255. then
  256.     echo shar: error transmitting "'pbmtox10wd.1'" '(should have been 691 characters)'
  257. fi
  258. fi # end of overwriting check
  259. echo shar: extracting "'pbmtoxwd.c'" '(3517 characters)'
  260. if test -f 'pbmtoxwd.c'
  261. then
  262.     echo shar: will not over-write existing file "'pbmtoxwd.c'"
  263. else
  264. sed 's/^X//' << \SHAR_EOF > 'pbmtoxwd.c'
  265. X/* pbmtoxwd.c - read a portable bitmap and produce an X11 window dump
  266. X**
  267. X** Copyright (C) 1988 by Jef Poskanzer.
  268. X**
  269. X** Permission to use, copy, modify, and distribute this software and its
  270. X** documentation for any purpose and without fee is hereby granted, provided
  271. X** that the above copyright notice appear in all copies and that both that
  272. X** copyright notice and this permission notice appear in supporting
  273. X** documentation.  This software is provided "as is" without express or
  274. X** implied warranty.
  275. X*/
  276. X
  277. X#include <stdio.h>
  278. X#ifdef    OS_SYSV
  279. X#include <string.h>
  280. X#else    OS_SYSV
  281. X#include <strings.h>
  282. X#endif    OS_SYSV
  283. X#include "pbm.h"
  284. X#include "x11wd.h"
  285. X
  286. Xmain( argc, argv )
  287. Xint argc;
  288. Xchar *argv[];
  289. X    {
  290. X    FILE *ifd;
  291. X    bit **bits;
  292. X    int rows, cols, padright, row, col;
  293. X    char name[100], *cp;
  294. X
  295. X    if ( argc > 2 )
  296. X    {
  297. X    fprintf( stderr, "usage:  %s [pbmfile]\n", argv[0] );
  298. X    exit( 1 );
  299. X    }
  300. X
  301. X    if ( argc == 2 )
  302. X    {
  303. X        ifd = fopen( argv[1], "r" );
  304. X        if ( ifd == NULL )
  305. X        {
  306. X        fprintf( stderr, "%s: can't open.\n", argv[1] );
  307. X        exit( 1 );
  308. X        }
  309. X    strcpy( name, argv[1] );
  310. X
  311. X#ifdef    OS_SYSV
  312. X    if ( ( cp = strchr( name, '.' ) ) != 0 )
  313. X#else    OS_SYSV
  314. X    if ( ( cp = index( name, '.' ) ) != 0 )
  315. X#endif    OS_SYSV
  316. X        *cp = '\0';
  317. X    }
  318. X    else
  319. X    {
  320. X    ifd = stdin;
  321. X    strcpy( name, "noname" );
  322. X    }
  323. X
  324. X    bits = pbm_readpbm( ifd, &cols, &rows );
  325. X
  326. X    if ( ifd != stdin )
  327. X    fclose( ifd );
  328. X    
  329. X    /* Compute padding to round cols up to the nearest multiple of 32. */
  330. X    padright = ( ( cols + 31 ) / 32 ) * 32 - cols;
  331. X
  332. X    putinit( cols, rows, name );
  333. X    for ( row = 0; row < rows; row++ )
  334. X    {
  335. X        for ( col = 0; col < cols; col++ )
  336. X        putbit( bits[row][col] );
  337. X    for ( col = 0; col < padright; col++ )
  338. X        putbit( 0 );
  339. X        }
  340. X    putrest( );
  341. X
  342. X    exit( 0 );
  343. X    }
  344. X
  345. X
  346. Xlong item;
  347. Xint bitsperitem, bitshift;
  348. X
  349. X
  350. Xputinit( cols, rows, name )
  351. Xint cols, rows;
  352. Xchar *name;
  353. X    {
  354. X    X11WDFileHeader h11;
  355. X    X11XColor color;
  356. X
  357. X    h11.header_size = sizeof(h11) + (strlen(name) + 1) * sizeof(char);
  358. X    h11.file_version = X11WD_FILE_VERSION;
  359. X    h11.pixmap_format = ZPixmap;
  360. X    h11.pixmap_depth = 1;
  361. X    h11.pixmap_width = cols;
  362. X    h11.pixmap_height = rows;
  363. X    h11.xoffset = 0;
  364. X    h11.byte_order = MSBFirst;
  365. X    h11.bitmap_unit = 32;
  366. X    h11.bitmap_bit_order = MSBFirst;
  367. X    h11.bitmap_pad = 32;
  368. X    h11.bits_per_pixel = 1;
  369. X    h11.bytes_per_line = ( ( cols + 31 ) / 32 ) * 4;
  370. X    h11.visual_class = 0;
  371. X    h11.red_mask = 0;
  372. X    h11.green_mask = 0;
  373. X    h11.blue_mask = 0;
  374. X    h11.bits_per_rgb = 1;
  375. X    h11.colormap_entries = 2;
  376. X    h11.ncolors = 2;
  377. X    h11.window_width = cols;
  378. X    h11.window_height = rows;
  379. X    h11.window_x = 0;            /* arbitrary */
  380. X    h11.window_y = 0;            /* arbitrary */
  381. X    h11.window_bdrwidth = 0;
  382. X    fwrite( &h11, sizeof(h11), 1, stdout );
  383. X
  384. X    fwrite( name, sizeof(char), strlen(name) + 1, stdout );
  385. X
  386. X    color.pixel = 0;
  387. X    color.red = 65535;
  388. X    color.green = 65535;
  389. X    color.blue = 65535;
  390. X    color.flags = 7;
  391. X    color.pad = 0;
  392. X    fwrite( &color, sizeof(color), 1, stdout );
  393. X    color.pixel = 1;
  394. X    color.red = 0;
  395. X    color.green = 0;
  396. X    color.blue = 0;
  397. X    fwrite( &color, sizeof(color), 1, stdout );
  398. X
  399. X    item = 0;
  400. X    bitsperitem = 0;
  401. X    bitshift = 31;
  402. X    }
  403. X
  404. Xputbit( b )
  405. Xbit b;
  406. X    {
  407. X    if ( bitsperitem == 32 )
  408. X    putitem( );
  409. X    bitsperitem++;
  410. X    if ( b )
  411. X    item += 1 << bitshift;
  412. X    bitshift--;
  413. X    }
  414. X
  415. Xputrest( )
  416. X    {
  417. X    if ( bitsperitem > 0 )
  418. X    putitem( );
  419. X    }
  420. X
  421. Xputitem( )
  422. X    {
  423. X    fwrite( &item, sizeof(item), 1, stdout );
  424. X    item = 0;
  425. X    bitsperitem = 0;
  426. X    bitshift = 31;
  427. X    }
  428. SHAR_EOF
  429. if test 3517 -ne "`wc -c < 'pbmtoxwd.c'`"
  430. then
  431.     echo shar: error transmitting "'pbmtoxwd.c'" '(should have been 3517 characters)'
  432. fi
  433. fi # end of overwriting check
  434. echo shar: extracting "'pbmtoxwd.1'" '(742 characters)'
  435. if test -f 'pbmtoxwd.1'
  436. then
  437.     echo shar: will not over-write existing file "'pbmtoxwd.1'"
  438. else
  439. sed 's/^X//' << \SHAR_EOF > 'pbmtoxwd.1'
  440. X.TH pbmtoxwd 1 "31 August 1988"
  441. X.SH NAME
  442. Xpbmtoxwd - convert portable bitmaps into X11 window dumps
  443. X.SH SYNOPSIS
  444. Xpbmtoxwd [pbmfile]
  445. X.SH DESCRIPTION
  446. XReads a portable bitmap as input.
  447. XProduces an X11 window dump as output.
  448. XThis window dump can be displayed using the xwud tool.
  449. X.SH "SEE ALSO"
  450. Xpbmtox10wd(1), xwdtopbm(1), pbm(5)
  451. X.SH AUTHOR
  452. XCopyright (C) 1988 by Jef Poskanzer.
  453. X
  454. XPermission to use, copy, modify, and distribute this software and its
  455. Xdocumentation for any purpose and without fee is hereby granted, provided
  456. Xthat the above copyright notice appear in all copies and that both that
  457. Xcopyright notice and this permission notice appear in supporting
  458. Xdocumentation.  This software is provided "as is" without express or
  459. Ximplied warranty.
  460. SHAR_EOF
  461. if test 742 -ne "`wc -c < 'pbmtoxwd.1'`"
  462. then
  463.     echo shar: error transmitting "'pbmtoxwd.1'" '(should have been 742 characters)'
  464. fi
  465. fi # end of overwriting check
  466. echo shar: extracting "'brushtopbm.c'" '(2333 characters)'
  467. if test -f 'brushtopbm.c'
  468. then
  469.     echo shar: will not over-write existing file "'brushtopbm.c'"
  470. else
  471. sed 's/^X//' << \SHAR_EOF > 'brushtopbm.c'
  472. X/* brushtopbm.c - read a doodle brush file and write a portable bitmap
  473. X**
  474. X** Copyright (C) 1988 by Jef Poskanzer.
  475. X**
  476. X** Permission to use, copy, modify, and distribute this software and its
  477. X** documentation for any purpose and without fee is hereby granted, provided
  478. X** that the above copyright notice appear in all copies and that both that
  479. X** copyright notice and this permission notice appear in supporting
  480. X** documentation.  This software is provided "as is" without express or
  481. X** implied warranty.
  482. X*/
  483. X
  484. X#include <stdio.h>
  485. X#include "pbm.h"
  486. X
  487. Xmain( argc, argv )
  488. Xint argc;
  489. Xchar *argv[];
  490. X    {
  491. X    FILE *ifd;
  492. X    bit **bits, getbit();
  493. X    int rows, cols, padright, row, col;
  494. X
  495. X    if ( argc > 2 )
  496. X    {
  497. X    fprintf( stderr, "usage:  %s [brushfile]\n", argv[0] );
  498. X    exit( 1 );
  499. X    }
  500. X
  501. X    if ( argc == 2 )
  502. X    {
  503. X        ifd = fopen( argv[1], "r" );
  504. X        if ( ifd == NULL )
  505. X        {
  506. X        fprintf( stderr, "%s: can't open.\n", argv[1] );
  507. X        exit( 1 );
  508. X        }
  509. X    }
  510. X    else
  511. X    ifd = stdin;
  512. X
  513. X    getinit( ifd, &cols, &rows );
  514. X
  515. X    bits = pbm_allocarray( cols, rows );
  516. X
  517. X    /* Compute padding to round cols up to the next multiple of 16. */
  518. X    padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  519. X
  520. X    for ( row = 0; row < rows; row++ )
  521. X    {
  522. X    /* Get data. */
  523. X        for ( col = 0; col < cols; col++ )
  524. X        bits[row][col] = getbit( ifd );
  525. X    /* Discard line padding. */
  526. X        for ( col = 0; col < padright; col++ )
  527. X        (void) getbit( ifd );
  528. X    }
  529. X
  530. X    if ( ifd != stdin )
  531. X    fclose( ifd );
  532. X    
  533. X    pbm_writepbm( stdout, bits, cols, rows );
  534. X
  535. X    exit( 0 );
  536. X    }
  537. X
  538. X
  539. Xint item, bitsperitem, bitshift;
  540. X
  541. Xgetinit( file, colp, rowp )
  542. XFILE *file;
  543. Xint *colp, *rowp;
  544. X    {
  545. X    int i;
  546. X
  547. X    if ( getc( file ) != 1 )
  548. X    {
  549. X    fprintf( stderr, "Bad magic number 1.\n" );
  550. X    exit( 1 );
  551. X    }
  552. X    if ( getc( file ) != 0 )
  553. X    {
  554. X    fprintf( stderr, "Bad magic number 2.\n" );
  555. X    exit( 1 );
  556. X    }
  557. X    *colp = getc( file ) << 8;
  558. X    *colp += getc( file );
  559. X    *rowp = getc( file ) << 8;
  560. X    *rowp += getc( file );
  561. X    bitsperitem = 8;
  562. X
  563. X    /* Junk rest of header. */
  564. X    for ( i = 0; i < 10; i++ )  /* 10 is just a guess at the header size */
  565. X    (void) getc( file );
  566. X    }
  567. X
  568. Xbit
  569. Xgetbit( file )
  570. XFILE *file;
  571. X    {
  572. X    bit b;
  573. X
  574. X    if ( bitsperitem == 8 )
  575. X    {
  576. X    item = getc( file );
  577. X    bitsperitem = 0;
  578. X    bitshift = 7;
  579. X    }
  580. X    bitsperitem++;
  581. X    b = 1 - ( ( item >> bitshift) & 1 );
  582. X    bitshift--;
  583. X    return b;
  584. X    }
  585. SHAR_EOF
  586. if test 2333 -ne "`wc -c < 'brushtopbm.c'`"
  587. then
  588.     echo shar: error transmitting "'brushtopbm.c'" '(should have been 2333 characters)'
  589. fi
  590. fi # end of overwriting check
  591. echo shar: extracting "'brushtopbm.1'" '(729 characters)'
  592. if test -f 'brushtopbm.1'
  593. then
  594.     echo shar: will not over-write existing file "'brushtopbm.1'"
  595. else
  596. sed 's/^X//' << \SHAR_EOF > 'brushtopbm.1'
  597. X.TH brushtopbm 1 "28 August 1988"
  598. X.SH NAME
  599. Xbrushtopbm - convert doodle brush files into portable bitmaps
  600. X.SH SYNOPSIS
  601. Xbrushtopbm [brushfile]
  602. X.SH DESCRIPTION
  603. XReads a Xerox doodle brush file as input.
  604. XProduces a portable bitmap as output.
  605. X.LP
  606. XNote that there is currently no pbmtobrush tool.
  607. X.SH "SEE ALSO"
  608. Xpbm(5)
  609. X.SH AUTHOR
  610. XCopyright (C) 1988 by Jef Poskanzer.
  611. X
  612. XPermission to use, copy, modify, and distribute this software and its
  613. Xdocumentation for any purpose and without fee is hereby granted, provided
  614. Xthat the above copyright notice appear in all copies and that both that
  615. Xcopyright notice and this permission notice appear in supporting
  616. Xdocumentation.  This software is provided "as is" without express or
  617. Ximplied warranty.
  618. SHAR_EOF
  619. if test 729 -ne "`wc -c < 'brushtopbm.1'`"
  620. then
  621.     echo shar: error transmitting "'brushtopbm.1'" '(should have been 729 characters)'
  622. fi
  623. fi # end of overwriting check
  624. echo shar: extracting "'libpbm1.c'" '(789 characters)'
  625. if test -f 'libpbm1.c'
  626. then
  627.     echo shar: will not over-write existing file "'libpbm1.c'"
  628. else
  629. sed 's/^X//' << \SHAR_EOF > 'libpbm1.c'
  630. X/* libpbm1.c - pbm utility library part 1
  631. X**
  632. X** Copyright (C) 1988 by Jef Poskanzer.
  633. X**
  634. X** Permission to use, copy, modify, and distribute this software and its
  635. X** documentation for any purpose and without fee is hereby granted, provided
  636. X** that the above copyright notice appear in all copies and that both that
  637. X** copyright notice and this permission notice appear in supporting
  638. X** documentation.  This software is provided "as is" without express or
  639. X** implied warranty.
  640. X*/
  641. X
  642. X#include <stdio.h>
  643. X#include "pbm.h"
  644. X#include "libpbm.h"
  645. X
  646. X
  647. Xbit **
  648. Xpbm_allocarray( cols, rows )
  649. Xint cols, rows;
  650. X    {
  651. X    bit **bits;
  652. X    int i;
  653. X
  654. X    bits = (bit **) malloc( rows * sizeof( bit *) );
  655. X    for ( i = 0; i < rows; i++ )
  656. X    {
  657. X    bits[i] = (bit *) malloc( cols * sizeof( bit ) );
  658. X    }
  659. X
  660. X    return bits;
  661. X    }
  662. SHAR_EOF
  663. if test 789 -ne "`wc -c < 'libpbm1.c'`"
  664. then
  665.     echo shar: error transmitting "'libpbm1.c'" '(should have been 789 characters)'
  666. fi
  667. fi # end of overwriting check
  668. echo shar: extracting "'libpbm2.c'" '(2492 characters)'
  669. if test -f 'libpbm2.c'
  670. then
  671.     echo shar: will not over-write existing file "'libpbm2.c'"
  672. else
  673. sed 's/^X//' << \SHAR_EOF > 'libpbm2.c'
  674. X/* libpbm2.c - pbm utility library part 2
  675. X**
  676. X** Copyright (C) 1988 by Jef Poskanzer.
  677. X**
  678. X** Permission to use, copy, modify, and distribute this software and its
  679. X** documentation for any purpose and without fee is hereby granted, provided
  680. X** that the above copyright notice appear in all copies and that both that
  681. X** copyright notice and this permission notice appear in supporting
  682. X** documentation.  This software is provided "as is" without express or
  683. X** implied warranty.
  684. X*/
  685. X
  686. X#include <stdio.h>
  687. X#include "pbm.h"
  688. X#include "libpbm.h"
  689. X
  690. X
  691. Xstatic char
  692. Xpbm_getc( file )
  693. XFILE *file;
  694. X    {
  695. X    int ich;
  696. X    char ch;
  697. X
  698. X    ich = getc( file );
  699. X    if ( ich == NULL )
  700. X    {
  701. X    fprintf( stderr, "Premature EOF.\n" );
  702. X    exit( 1 );
  703. X    }
  704. X    ch = (char) ich;
  705. X    
  706. X    if ( ch == '#' )
  707. X    {
  708. X    do
  709. X        {
  710. X        ich = getc( file );
  711. X        if ( ich == NULL )
  712. X        {
  713. X        fprintf( stderr, "Premature EOF.\n" );
  714. X        exit( 1 );
  715. X        }
  716. X        ch = (char) ich;
  717. X        }
  718. X    while ( ch != '\n' );
  719. X    }
  720. X
  721. X    return ch;
  722. X    }
  723. X
  724. Xstatic int
  725. Xpbm_getint( file )
  726. XFILE *file;
  727. X    {
  728. X    char ch;
  729. X    int i;
  730. X
  731. X    do
  732. X    {
  733. X    ch = pbm_getc( file );
  734. X    }
  735. X    while ( ch == ' ' || ch == '\t' || ch == '\n' );
  736. X
  737. X    if ( ch < '0' || ch > '9' )
  738. X    {
  739. X    fprintf( stderr, "Junk in file where an integer should be!\n" );
  740. X    exit( 1 );
  741. X    }
  742. X
  743. X    i = 0;
  744. X    do
  745. X    {
  746. X    i = i * 10 + ch - '0';
  747. X    ch = pbm_getc( file );
  748. X        }
  749. X    while ( ch >= '0' && ch <= '9' );
  750. X
  751. X    return i;
  752. X    }
  753. X
  754. Xstatic bit
  755. Xpbm_getbit( file )
  756. XFILE *file;
  757. X    {
  758. X    char ch;
  759. X
  760. X    do
  761. X    {
  762. X    ch = pbm_getc( file );
  763. X    }
  764. X    while ( ch == ' ' || ch == '\t' || ch == '\n' );
  765. X
  766. X    if ( ch != '0' && ch != '1' )
  767. X    {
  768. X    fprintf( stderr, "Junk in file where bits should be!\n" );
  769. X    exit( 1 );
  770. X    }
  771. X
  772. X    return ( ch == '1' ) ? 1 : 0;
  773. X    }
  774. X
  775. X
  776. Xbit **
  777. Xpbm_readpbm( file, colsP, rowsP )
  778. XFILE *file;
  779. Xint *colsP, *rowsP;
  780. X    {
  781. X    int ich1, ich2;
  782. X    bit **bits;
  783. X    int row, col;
  784. X
  785. X    /* Check for magic number. */
  786. X    ich1 = getc( file );
  787. X    if ( ich1 == NULL )
  788. X    {
  789. X    fprintf( stderr, "Premature EOF.\n" );
  790. X    exit( 1 );
  791. X    }
  792. X    ich2 = getc( file );
  793. X    if ( ich2 == NULL )
  794. X    {
  795. X    fprintf( stderr, "Premature EOF.\n" );
  796. X    exit( 1 );
  797. X    }
  798. X    if ( ich1 != PBM_MAGIC1 || ich2 != PBM_MAGIC2 )
  799. X    {
  800. X    fprintf( stderr, "Bad magic number - not a pbm file.\n" );
  801. X    exit( 1 );
  802. X    }
  803. X
  804. X    /* Read size. */
  805. X    *colsP = pbm_getint( file );
  806. X    *rowsP = pbm_getint( file );
  807. X
  808. X    bits = pbm_allocarray( *colsP, *rowsP );
  809. X
  810. X    for ( row = 0; row < *rowsP; row++ )
  811. X        for ( col = 0; col < *colsP; col++ )
  812. X        bits[row][col] = pbm_getbit( file );
  813. X
  814. X    return bits;
  815. X    }
  816. SHAR_EOF
  817. if test 2492 -ne "`wc -c < 'libpbm2.c'`"
  818. then
  819.     echo shar: error transmitting "'libpbm2.c'" '(should have been 2492 characters)'
  820. fi
  821. fi # end of overwriting check
  822. echo shar: extracting "'libpbm3.c'" '(1018 characters)'
  823. if test -f 'libpbm3.c'
  824. then
  825.     echo shar: will not over-write existing file "'libpbm3.c'"
  826. else
  827. sed 's/^X//' << \SHAR_EOF > 'libpbm3.c'
  828. X/* libpbm3.c - pbm utility library part 3
  829. X**
  830. X** Copyright (C) 1988 by Jef Poskanzer.
  831. X**
  832. X** Permission to use, copy, modify, and distribute this software and its
  833. X** documentation for any purpose and without fee is hereby granted, provided
  834. X** that the above copyright notice appear in all copies and that both that
  835. X** copyright notice and this permission notice appear in supporting
  836. X** documentation.  This software is provided "as is" without express or
  837. X** implied warranty.
  838. X*/
  839. X
  840. X#include <stdio.h>
  841. X#include "pbm.h"
  842. X#include "libpbm.h"
  843. X
  844. X
  845. Xpbm_writepbm( file, bits, cols, rows )
  846. XFILE *file;
  847. Xbit **bits;
  848. Xint cols, rows;
  849. X    {
  850. X    int row, col, linecount;
  851. X
  852. X    fprintf( file, "%c%c\n%d %d\n", PBM_MAGIC1, PBM_MAGIC2, cols, rows );
  853. X
  854. X    for ( row = 0; row < rows; row++ )
  855. X    {
  856. X    linecount = 0;
  857. X        for ( col = 0; col < cols; col++ )
  858. X        {
  859. X        if ( linecount >= 70 )
  860. X        {
  861. X        putc( '\n', file );
  862. X        linecount = 0;
  863. X        }
  864. X        putc( bits[row][col] ? '1' : '0', file );
  865. X        linecount++;
  866. X        }
  867. X    putc( '\n', file );
  868. X        }
  869. X    }
  870. SHAR_EOF
  871. if test 1018 -ne "`wc -c < 'libpbm3.c'`"
  872. then
  873.     echo shar: error transmitting "'libpbm3.c'" '(should have been 1018 characters)'
  874. fi
  875. fi # end of overwriting check
  876. echo shar: extracting "'libpbm4.c'" '(2929 characters)'
  877. if test -f 'libpbm4.c'
  878. then
  879.     echo shar: will not over-write existing file "'libpbm4.c'"
  880. else
  881. sed 's/^X//' << \SHAR_EOF > 'libpbm4.c'
  882. X/* libpbm4.c - pbm utility library part 4
  883. X**
  884. X** Copyright (C) 1988 by Jef Poskanzer.
  885. X**
  886. X** Permission to use, copy, modify, and distribute this software and its
  887. X** documentation for any purpose and without fee is hereby granted, provided
  888. X** that the above copyright notice appear in all copies and that both that
  889. X** copyright notice and this permission notice appear in supporting
  890. X** documentation.  This software is provided "as is" without express or
  891. X** implied warranty.
  892. X*/
  893. X
  894. X#include <stdio.h>
  895. X#include "pbm.h"
  896. X#include "libpbm.h"
  897. X
  898. X
  899. Xstatic int version, item, bitsinitem, bitshift;
  900. Xstatic int bitspercount, count, repeatbits, repeatindex;
  901. Xstatic bit repeat, repeatbuf[MAX_REPEATBITS];
  902. Xstatic FILE *cbm_file;
  903. X
  904. X
  905. Xstatic
  906. Xcbm_getinit( file, colsP, rowsP )
  907. XFILE *file;
  908. Xint *colsP, *rowsP;
  909. X    {
  910. X    int magic2;
  911. X
  912. X    if ( getc( file ) != CBM_MAGIC1 )
  913. X    {
  914. X    fprintf( stderr, "Bad first magic number.\n" );
  915. X    exit( 1 );
  916. X    }
  917. X    if ( ( magic2 = getc( file ) ) != OCBM_MAGIC2 && magic2 != NCBM_MAGIC2 )
  918. X    {
  919. X    fprintf( stderr, "Bad second magic number.\n" );
  920. X    exit( 1 );
  921. X    }
  922. X
  923. X    if ( magic2 == OCBM_MAGIC2 )
  924. X    version = VERSION_OLDCBM;
  925. X    else
  926. X    {
  927. X    version = getc( file );
  928. X    switch ( version )
  929. X        {
  930. X        case VERSION_RUNLEN:
  931. X        bitspercount = getc( file );
  932. X        repeatbits = getc( file );
  933. X        count = 0;
  934. X        break;
  935. X
  936. X        default:
  937. X        fprintf( stderr, "Unknown cbm version %d.\n", version );
  938. X        exit( 1 );
  939. X        }
  940. X    }
  941. X
  942. X    *colsP = getc( file ) << 8;
  943. X    *colsP += getc( file );
  944. X    *rowsP = getc( file ) << 8;
  945. X    *rowsP += getc( file );
  946. X
  947. X    bitsinitem = 8;
  948. X    cbm_file = file;
  949. X    }
  950. X
  951. Xstatic bit
  952. Xcbm_getbit( )
  953. X    {
  954. X    bit b;
  955. X
  956. X    if ( bitsinitem == 8 )
  957. X    {
  958. X    item = getc( cbm_file );
  959. X    bitsinitem = 0;
  960. X    bitshift = 7;
  961. X    }
  962. X    bitsinitem++;
  963. X    b = ( item >> bitshift) & 1;
  964. X    bitshift--;
  965. X    return b;
  966. X    }
  967. X
  968. Xstatic bit
  969. Xcbm_getrlbit( )
  970. X    {
  971. X    int i;
  972. X
  973. X    if ( count == 0 )
  974. X    {
  975. X    repeat = cbm_getbit( );
  976. X
  977. X    for ( i = 0; i < bitspercount; i++ )
  978. X        count = ( count << 1 ) + cbm_getbit( );
  979. X    if ( count == 0 )
  980. X        {
  981. X        fprintf( stderr, "Error - zero count in cbm file.\n" );
  982. X        exit( 1 );
  983. X        }
  984. X
  985. X    if ( repeat )
  986. X        {
  987. X        for ( i = 0; i < repeatbits; i++ )
  988. X        repeatbuf[i] = cbm_getbit( );
  989. X        repeatindex = repeatbits - 1;
  990. X        }
  991. X    }
  992. X
  993. X    count--;
  994. X    if ( repeat )
  995. X    {
  996. X    repeatindex = ( repeatindex + 1 ) % repeatbits;
  997. X    return repeatbuf[repeatindex];
  998. X    }
  999. X    else
  1000. X    return cbm_getbit( );
  1001. X    }
  1002. X
  1003. Xbit **
  1004. Xpbm_readcbm( file, colsP, rowsP )
  1005. XFILE *file;
  1006. Xint *colsP, *rowsP;
  1007. X    {
  1008. X    bit **bits;
  1009. X    int row, col;
  1010. X
  1011. X    cbm_getinit( file, colsP, rowsP );
  1012. X
  1013. X    bits = pbm_allocarray( *colsP, *rowsP );
  1014. X
  1015. X    switch ( version )
  1016. X    {
  1017. X    case VERSION_OLDCBM:
  1018. X    for ( row = 0; row < *rowsP; row++ )
  1019. X        for ( col = 0; col < *colsP; col++ )
  1020. X        bits[row][col] = cbm_getbit( );
  1021. X    break;
  1022. X
  1023. X    case VERSION_RUNLEN:
  1024. X    for ( row = 0; row < *rowsP; row++ )
  1025. X        for ( col = 0; col < *colsP; col++ )
  1026. X        bits[row][col] = cbm_getrlbit( );
  1027. X    break;
  1028. X    }
  1029. X
  1030. X    return bits;
  1031. X    }
  1032. SHAR_EOF
  1033. if test 2929 -ne "`wc -c < 'libpbm4.c'`"
  1034. then
  1035.     echo shar: error transmitting "'libpbm4.c'" '(should have been 2929 characters)'
  1036. fi
  1037. fi # end of overwriting check
  1038. echo shar: extracting "'libpbm5.c'" '(4574 characters)'
  1039. if test -f 'libpbm5.c'
  1040. then
  1041.     echo shar: will not over-write existing file "'libpbm5.c'"
  1042. else
  1043. sed 's/^X//' << \SHAR_EOF > 'libpbm5.c'
  1044. X/* libpbm5.c - pbm utility library part 5
  1045. X**
  1046. X** Copyright (C) 1988 by Jef Poskanzer.
  1047. X**
  1048. X** Permission to use, copy, modify, and distribute this software and its
  1049. X** documentation for any purpose and without fee is hereby granted, provided
  1050. X** that the above copyright notice appear in all copies and that both that
  1051. X** copyright notice and this permission notice appear in supporting
  1052. X** documentation.  This software is provided "as is" without express or
  1053. X** implied warranty.
  1054. X*/
  1055. X
  1056. X#include <stdio.h>
  1057. X#include "pbm.h"
  1058. X#include "libpbm.h"
  1059. X
  1060. X
  1061. Xstatic int item, bitsinitem, bitshift;
  1062. Xstatic int bitspercount, count, maxcount, repeatbits, repeatcount, repeatindex;
  1063. Xstatic bit repeat, bitbuf[MAX_MAXCOUNT], repeatbuf[MAX_REPEATBITS];
  1064. Xstatic FILE *cbm_file;
  1065. X
  1066. X
  1067. Xstatic
  1068. Xcbm_putinit( cols, rows, file )
  1069. Xint cols, rows;
  1070. XFILE *file;
  1071. X    {
  1072. X    putc( (char) CBM_MAGIC1, file );
  1073. X    putc( (char) NCBM_MAGIC2, file );
  1074. X
  1075. X    putc( (char) VERSION_RUNLEN, file );
  1076. X
  1077. X    bitspercount = DEFAULT_BITSPERCOUNT;
  1078. X    maxcount = ( 1 << bitspercount ) - 1;
  1079. X    putc( (char) bitspercount, file );
  1080. X
  1081. X    repeatbits = DEFAULT_REPEATBITS;
  1082. X    putc( (char) repeatbits, file );
  1083. X
  1084. X    putc( (char) ( ( cols >> 8 ) & 0xff ), file );
  1085. X    putc( (char) ( cols & 0xff ), file );
  1086. X    putc( (char) ( ( rows >> 8 ) & 0xff ), file );
  1087. X    putc( (char) ( rows & 0xff ), file );
  1088. X
  1089. X    /* Initialize statics for run-length encoding. */
  1090. X    repeat = 1;
  1091. X    count = 0;
  1092. X    repeatindex = 0;
  1093. X
  1094. X    /* Initialize statics for bit-to-byte encoding. */
  1095. X    bitsinitem = 0;
  1096. X    item = 0;
  1097. X    bitshift = 7;
  1098. X    cbm_file = file;
  1099. X    }
  1100. X
  1101. Xstatic
  1102. Xcbm_putitem( )
  1103. X    {
  1104. X    putc( (char) item, cbm_file );
  1105. X    bitsinitem = 0;
  1106. X    item = 0;
  1107. X    bitshift = 7;
  1108. X    }
  1109. X
  1110. Xstatic
  1111. Xcbm_putbit( b )
  1112. Xbit b;
  1113. X    {
  1114. X    if ( bitsinitem == 8 )
  1115. X    cbm_putitem( );
  1116. X    bitsinitem++;
  1117. X    if ( b )
  1118. X    item += 1 << bitshift;
  1119. X    bitshift--;
  1120. X    }
  1121. X
  1122. Xstatic
  1123. Xcbm_putrest( )
  1124. X    {
  1125. X    if ( bitsinitem > 0 )
  1126. X    cbm_putitem( );
  1127. X    }
  1128. X
  1129. Xstatic
  1130. Xcbm_putrlbuffer( )
  1131. X    {
  1132. X    int i;
  1133. X
  1134. X    cbm_putbit( repeat );
  1135. X
  1136. X    for ( i = bitspercount - 1; i >= 0; i-- )
  1137. X    if ( count & ( 1 << i ) )
  1138. X        cbm_putbit( 1 );
  1139. X    else
  1140. X        cbm_putbit( 0 );
  1141. X
  1142. X    if ( repeat )
  1143. X    for ( i = 0; i < repeatbits; i++ )
  1144. X        cbm_putbit( repeatbuf[i] );
  1145. X    else
  1146. X    for ( i = 0; i < count; i++ )
  1147. X        cbm_putbit( bitbuf[i] );
  1148. X
  1149. X    repeat = 1;
  1150. X    count = 0;
  1151. X    }
  1152. X
  1153. Xstatic
  1154. Xcbm_putrlbit( b )
  1155. Xbit b;
  1156. X    {
  1157. X    int i;
  1158. X
  1159. X    if ( count == maxcount )
  1160. X    {
  1161. X    cbm_putrlbuffer( );
  1162. X    repeatindex = 0;
  1163. X    }
  1164. X
  1165. X    if ( repeat && count < repeatbits )
  1166. X    { /* Still initializing a repeat buf. */
  1167. X    bitbuf[count] = repeatbuf[repeatindex] = b;
  1168. X    count++;
  1169. X    repeatindex = ( repeatindex + 1 ) % repeatbits;
  1170. X    }
  1171. X    else if ( repeat )
  1172. X    { /* Repeating - watch for end of run. */
  1173. X    if ( b == repeatbuf[repeatindex] )
  1174. X        { /* Run continues. */
  1175. X        bitbuf[count] = b;
  1176. X        count++;
  1177. X        repeatindex = ( repeatindex + 1 ) % repeatbits;
  1178. X        }
  1179. X    else
  1180. X        { /* Run ended - is it long enough to dump? */
  1181. X        if ( count > bitspercount + 1 )
  1182. X        { /* Yes, dump a repeat-mode buffer and start a new one. */
  1183. X        cbm_putrlbuffer( );
  1184. X        repeatindex = 0;
  1185. X        bitbuf[count] = repeatbuf[repeatindex] = b;
  1186. X        count++;
  1187. X        repeatindex++;
  1188. X        }
  1189. X        else
  1190. X        { /* Not long enough - convert to non-repeat mode. */
  1191. X        repeat = 0;
  1192. X        bitbuf[count] = b;
  1193. X        count++;
  1194. X        for ( i = 0; i < repeatbits; i++ )
  1195. X            repeatbuf[i] = bitbuf[count - repeatbits + i];
  1196. X        repeatcount = repeatbits;
  1197. X        repeatindex = 0;
  1198. X        }
  1199. X        }
  1200. X    }
  1201. X    else
  1202. X    { /* Not repeating - watch for a run worth repeating. */
  1203. X    if ( b == repeatbuf[repeatindex] )
  1204. X        { /* Possible run continues. */
  1205. X        repeatcount++;
  1206. X        repeatindex = ( repeatindex + 1 ) % repeatbits;
  1207. X        if ( repeatcount > 2 + 2 * bitspercount + repeatbits )
  1208. X        { /* Long enough - dump non-repeat part and start repeat. */
  1209. X        count = count - ( repeatcount - 1 );
  1210. X        cbm_putrlbuffer( );
  1211. X        count = repeatcount;
  1212. X        /* ??? */
  1213. X        }
  1214. X        else
  1215. X        { /* Not long enough yet - continue as non-repeat buf. */
  1216. X        bitbuf[count] = b;
  1217. X        count++;
  1218. X        }
  1219. X        }
  1220. X    else
  1221. X        { /* Broken run. */
  1222. X        bitbuf[count] = b;
  1223. X        count++;
  1224. X        for ( i = 0; i < repeatbits; i++ )
  1225. X        repeatbuf[i] = bitbuf[count - repeatbits + i];
  1226. X        repeatcount = repeatbits;
  1227. X        repeatindex = 0;
  1228. X        }
  1229. X    }
  1230. X    }
  1231. X
  1232. Xstatic
  1233. Xcbm_putrlrest( )
  1234. X    {
  1235. X    if ( count > 0 )
  1236. X    cbm_putrlbuffer( );
  1237. X    cbm_putrest( );
  1238. X    }
  1239. X
  1240. X
  1241. Xpbm_writecbm( file, bits, cols, rows )
  1242. XFILE *file;
  1243. Xbit **bits;
  1244. Xint cols, rows;
  1245. X    {
  1246. X    int row, col, linecount;
  1247. X
  1248. X    cbm_putinit( cols, rows, file );
  1249. X
  1250. X    for ( row = 0; row < rows; row++ )
  1251. X        for ( col = 0; col < cols; col++ )
  1252. X        cbm_putrlbit( bits[row][col] );
  1253. X
  1254. X    cbm_putrlrest( );
  1255. X    }
  1256. SHAR_EOF
  1257. if test 4574 -ne "`wc -c < 'libpbm5.c'`"
  1258. then
  1259.     echo shar: error transmitting "'libpbm5.c'" '(should have been 4574 characters)'
  1260. fi
  1261. fi # end of overwriting check
  1262. echo shar: extracting "'giftopbm.c'" '(13188 characters)'
  1263. if test -f 'giftopbm.c'
  1264. then
  1265.     echo shar: will not over-write existing file "'giftopbm.c'"
  1266. else
  1267. sed 's/^X//' << \SHAR_EOF > 'giftopbm.c'
  1268. X/*-
  1269. X * gif2ras.c - Converts from a Compuserve GIF (tm) image to a Sun Raster image.
  1270. X *
  1271. X * Copyright (c) 1988 by Patrick J. Naughton
  1272. X *
  1273. X * Author: Patrick J. Naughton
  1274. X * naughton@wind.sun.com
  1275. X *
  1276. X * Permission to use, copy, modify, and distribute this software and its
  1277. X * documentation for any purpose and without fee is hereby granted,
  1278. X * provided that the above copyright notice appear in all copies and that
  1279. X * both that copyright notice and this permission notice appear in
  1280. X * supporting documentation.
  1281. X *
  1282. X * This file is provided AS IS with no warranties of any kind.  The author
  1283. X * shall have no liability with respect to the infringement of copyrights,
  1284. X * trade secrets or any patents by this file or any part thereof.  In no
  1285. X * event will the author be liable for any lost revenue or profits or
  1286. X * other special, indirect and consequential damages.
  1287. X *
  1288. X * Comments and additions should be sent to the author:
  1289. X *
  1290. X *                     Patrick J. Naughton
  1291. X *                     Sun Microsystems, Inc.
  1292. X *                     2550 Garcia Ave, MS 14-40
  1293. X *                     Mountain View, CA 94043
  1294. X *                     (415) 336-1080
  1295. X *
  1296. X * Revision History:
  1297. X * 28-Aug-88: Modified by Jef Poskanzer to output PBM instead of Sun raster.
  1298. X * 27-Jul-88: Updated to use libpixrect to fix 386i byteswapping problems.
  1299. X * 11-Apr-88: Converted to C and changed to write Sun rasterfiles.
  1300. X * 19-Jan-88: GIFSLOW.PAS posted to comp.graphics by Jim Briebel,
  1301. X *            a Turbo Pascal 4.0 program to painfully slowly display
  1302. X *            GIF images on an EGA equipped IBM-PC.
  1303. X *
  1304. X * Description:
  1305. X *   This program takes a Compuserve "Graphics Interchange Format" or "GIF"
  1306. X * file as input and writes a file known as a Sun rasterfile.  This datafile
  1307. X * can be loaded by the NeWS "readcanvas" operator and is of the same format
  1308. X * as the files in /usr/NeWS/smi/*.  Under X11R2 there is a program called
  1309. X * xraster to display these files.
  1310. X *
  1311. X * Portability:
  1312. X *   To make this program convert to some image format other than PBM
  1313. X * format simply seach for the tag "PBMS:" in the source and
  1314. X * replace these simple mechanisms with the appropriate ones for the
  1315. X * other output format.  I have marked all (six) PBM Specific pieces
  1316. X * of code with this comment.
  1317. X *
  1318. X * SS: compile with "cc -o gif2ras -O gif2ras.c -lpixrect"
  1319. X * PBMS: compile with "cc -o giftopbm -O giftopbm.c libpbm.a
  1320. X */
  1321. X
  1322. X#include <stdio.h>
  1323. X#ifdef notdefSS
  1324. X#include <pixrect/pixrect_hs.h> /* SS: main Pixrect header file */
  1325. X#endif notdefSS
  1326. X#include <sys/types.h>
  1327. X#include "pbm.h"        /* PBMS: main PBM header file */
  1328. X
  1329. Xtypedef int boolean;
  1330. X#define True (1)
  1331. X#define False (0)
  1332. X
  1333. X#define NEXTSHORT (*ptr++ + (0x100 * *ptr++))
  1334. X#define NEXTBYTE (*ptr++)
  1335. X#define IMAGESEP 0x2c
  1336. X#define INTERLACEMASK 0x40
  1337. X#define COLORMAPMASK 0x80
  1338. X
  1339. Xint BitOffset = 0,        /* Bit Offset of next code */
  1340. X    XC = 0, YC = 0,        /* Output X and Y coords of current pixel */
  1341. X    Pass = 0,            /* Used by output routine if interlaced pic */
  1342. X    OutCount = 0,        /* Decompressor output 'stack count' */
  1343. X    RWidth, RHeight,        /* screen dimensions */
  1344. X    Width, Height,        /* image dimensions */
  1345. X    LeftOfs, TopOfs,        /* image offset */
  1346. X    BitsPerPixel,        /* Bits per pixel, read from GIF header */
  1347. X    ColorMapSize,        /* number of colors */
  1348. X    CodeSize,            /* Code size, read from GIF header */
  1349. X    InitCodeSize,        /* Starting code size, used during Clear */
  1350. X    Code,            /* Value returned by ReadCode */
  1351. X    MaxCode,            /* limiting value for current code size */
  1352. X    ClearCode,            /* GIF clear code */
  1353. X    EOFCode,            /* GIF end-of-information code */
  1354. X    CurCode, OldCode, InCode,    /* Decompressor variables */
  1355. X    FirstFree,            /* First free code, generated per GIF spec */
  1356. X    FreeCode,            /* Decompressor, next free slot in hash table */
  1357. X    FinChar,            /* Decompressor variable */
  1358. X    BitMask,            /* AND mask for data size */
  1359. X    ReadMask;            /* Code AND mask for current code size */
  1360. X
  1361. Xboolean Interlace, HasColormap;
  1362. X
  1363. X#ifdef notdefSS
  1364. X/* SS: defined in pixrect/pixrect_hs.h */
  1365. XPixrect *Output;        /* The Sun Pixrect */
  1366. Xcolormap_t Colormap;        /* The Pixrect Colormap */
  1367. Xu_char *Image;            /* The result array */
  1368. X#endif notdefSS
  1369. X/* PBMS: defined in pbm.h */
  1370. Xbit **bits;            /* The PBM bit array */
  1371. X
  1372. Xu_char *RawGIF;            /* The heap array to hold it, raw */
  1373. Xu_char *Raster;            /* The raster data stream, unblocked */
  1374. X
  1375. X    /* The hash table used by the decompressor */
  1376. Xint Prefix[4096];
  1377. Xint Suffix[4096];
  1378. X
  1379. X    /* An output array used by the decompressor */
  1380. Xint OutCode[1025];
  1381. X
  1382. X    /* The color map, read from the GIF header */
  1383. Xu_char Red[256], Green[256], Blue[256];
  1384. X
  1385. Xchar *id = "GIF87a";
  1386. X
  1387. Xchar *pname;            /* program name (used for error messages) */
  1388. Xvoid
  1389. Xerror(s1, s2)
  1390. Xchar *s1, *s2;
  1391. X{
  1392. X    fprintf(stderr, s1, pname, s2);
  1393. X    exit(1);
  1394. X}
  1395. X
  1396. X
  1397. Xmain(argc, argv)
  1398. Xint argc;
  1399. Xchar *argv[];
  1400. X{
  1401. XFILE *fp;
  1402. Xchar *infname = argv[1];
  1403. Xchar *outfname = argv[2];
  1404. Xint filesize;
  1405. Xregister u_char ch, ch1;
  1406. Xregister u_char *ptr, *ptr1;
  1407. Xregister int i;
  1408. X
  1409. X    setbuf(stderr, NULL);
  1410. X    pname = argv[0];
  1411. X
  1412. X    if (argc < 3)
  1413. X    error("usage: %s GIFfile rasterfile\n", NULL);
  1414. X
  1415. X    if (!(fp = fopen(infname, "r")))
  1416. X    error("%s: %s not found.\n", infname);
  1417. X
  1418. X    /* find the size of the file */
  1419. X
  1420. X    fseek(fp, 0L, 2);
  1421. X    filesize = ftell(fp);
  1422. X    fseek(fp, 0L, 0);
  1423. X
  1424. X    if (!(ptr = RawGIF = (u_char *) malloc(filesize)))
  1425. X    error("%s: not enough memory to read gif file.\n", NULL);
  1426. X
  1427. X    if (!(Raster = (u_char *) malloc(filesize)))
  1428. X    error("%s: not enough memory to read gif file.\n", NULL);
  1429. X
  1430. X    fread(ptr, filesize, 1, fp);
  1431. X
  1432. X    if (strncmp(ptr, id, 6))
  1433. X    error("%s: %s is not a GIF file.\n", infname);
  1434. X    ptr += 6;
  1435. X
  1436. X/* Get variables from the GIF screen descriptor */
  1437. X
  1438. X    RWidth = NEXTSHORT;        /* screen dimensions... not used. */
  1439. X    RHeight = NEXTSHORT;
  1440. X
  1441. X    ch = NEXTBYTE;
  1442. X    HasColormap = ((ch & COLORMAPMASK) ? True : False);
  1443. X
  1444. X    BitsPerPixel = (ch & 7) + 1;
  1445. X    ColorMapSize = 1 << BitsPerPixel;
  1446. X    BitMask = ColorMapSize - 1;
  1447. X
  1448. X    ch = NEXTBYTE;        /* background color... not used. */
  1449. X
  1450. X    if (NEXTBYTE)        /* supposed to be NULL */
  1451. X    error("%s: %s is a corrupt GIF file.\n", infname);
  1452. X
  1453. X/* Read in global colormap. */
  1454. X
  1455. X    if (HasColormap) {
  1456. X    fprintf(stderr, "%s is %d bits per pixel, (%d colors).\n",
  1457. X        infname, BitsPerPixel, ColorMapSize);
  1458. X    for (i = 0; i < ColorMapSize; i++) {
  1459. X        Red[i] = NEXTBYTE;
  1460. X        Green[i] = NEXTBYTE;
  1461. X        Blue[i] = NEXTBYTE;
  1462. X    }
  1463. X
  1464. X#ifdef notdefSS
  1465. X/* SS: Fill in the Pixrect colormap struct */
  1466. X    Colormap.type = RMT_EQUAL_RGB;
  1467. X    Colormap.length = ColorMapSize;
  1468. X    Colormap.map[0] = Red;
  1469. X    Colormap.map[1] = Green;
  1470. X    Colormap.map[2] = Blue;
  1471. X#endif notdefSS
  1472. X    /* PBMS: PBM only handles bitmaps.  Reject any pixmaps here. */
  1473. X    if (BitsPerPixel != 1)
  1474. X        error("%s: %s has more than one bit per pixel - it's not a bitmap.\n", infname);
  1475. X    }
  1476. X    else error("%s: %s does not have a colormap.\n", infname);
  1477. X
  1478. X
  1479. X/* Check for image seperator */
  1480. X
  1481. X    if (NEXTBYTE != IMAGESEP)
  1482. X    error("%s: %s is a corrupt GIF file.\n", infname);
  1483. X
  1484. X/* Now read in values from the image descriptor */
  1485. X
  1486. X    LeftOfs = NEXTSHORT;
  1487. X    TopOfs = NEXTSHORT;
  1488. X    Width = NEXTSHORT;
  1489. X    Height = NEXTSHORT;
  1490. X    Interlace = ((NEXTBYTE & INTERLACEMASK) ? True : False);
  1491. X
  1492. X    fprintf(stderr, "Reading a %d by %d %sinterlaced image...",
  1493. X        Width, Height, (Interlace) ? "" : "non-");
  1494. X    
  1495. X
  1496. X/* Note that I ignore the possible existence of a local color map.
  1497. X * I'm told there aren't many files around that use them, and the spec
  1498. X * says it's defined for future use.  This could lead to an error
  1499. X * reading some files. 
  1500. X */
  1501. X
  1502. X/* Start reading the raster data. First we get the intial code size
  1503. X * and compute decompressor constant values, based on this code size.
  1504. X */
  1505. X
  1506. X    CodeSize = NEXTBYTE;
  1507. X    ClearCode = (1 << CodeSize);
  1508. X    EOFCode = ClearCode + 1;
  1509. X    FreeCode = FirstFree = ClearCode + 2;
  1510. X
  1511. X/* The GIF spec has it that the code size is the code size used to
  1512. X * compute the above values is the code size given in the file, but the
  1513. X * code size used in compression/decompression is the code size given in
  1514. X * the file plus one. (thus the ++).
  1515. X */
  1516. X
  1517. X    CodeSize++;
  1518. X    InitCodeSize = CodeSize;
  1519. X    MaxCode = (1 << CodeSize);
  1520. X    ReadMask = MaxCode - 1;
  1521. X
  1522. X/* Read the raster data.  Here we just transpose it from the GIF array
  1523. X * to the Raster array, turning it from a series of blocks into one long
  1524. X * data stream, which makes life much easier for ReadCode().
  1525. X */
  1526. X
  1527. X    ptr1 = Raster;
  1528. X    do {
  1529. X    ch = ch1 = NEXTBYTE;
  1530. X    while (ch--) *ptr1++ = NEXTBYTE;
  1531. X    } while(ch1);
  1532. X
  1533. X    free(RawGIF);        /* We're done with the raw data now... */
  1534. X
  1535. X    fprintf(stderr, "done.\n");
  1536. X    fprintf(stderr, "Decompressing...");
  1537. X
  1538. X
  1539. X#ifdef notdefSS
  1540. X/* SS: Allocate the Sun Pixrect and make "Image" point to the image data. */
  1541. X    Output = mem_create(Width, Height, 8);
  1542. X    if (Output == (Pixrect *) NULL)
  1543. X    error("%s: not enough memory for output data.\n", NULL);
  1544. X    Image = (u_char *) mpr_d(Output)->md_image;
  1545. X#endif notdefSS
  1546. X/* PBMS: Allocate the PBM bit array. */
  1547. X    bits = pbm_allocarray(Width, Height);
  1548. X
  1549. X
  1550. X/* Decompress the file, continuing until you see the GIF EOF code.
  1551. X * One obvious enhancement is to add checking for corrupt files here.
  1552. X */
  1553. X
  1554. X    Code = ReadCode();
  1555. X    while (Code != EOFCode) {
  1556. X
  1557. X/* Clear code sets everything back to its initial value, then reads the
  1558. X * immediately subsequent code as uncompressed data.
  1559. X */
  1560. X
  1561. X    if (Code == ClearCode) {
  1562. X        CodeSize = InitCodeSize;
  1563. X        MaxCode = (1 << CodeSize);
  1564. X        ReadMask = MaxCode - 1;
  1565. X        FreeCode = FirstFree;
  1566. X        CurCode = OldCode = Code = ReadCode();
  1567. X        FinChar = CurCode & BitMask;
  1568. X        AddToPixel(FinChar);
  1569. X    }
  1570. X    else {
  1571. X
  1572. X/* If not a clear code, then must be data: save same as CurCode and InCode */
  1573. X
  1574. X        CurCode = InCode = Code;
  1575. X
  1576. X/* If greater or equal to FreeCode, not in the hash table yet;
  1577. X * repeat the last character decoded
  1578. X */
  1579. X
  1580. X        if (CurCode >= FreeCode) {
  1581. X        CurCode = OldCode;
  1582. X        OutCode[OutCount++] = FinChar;
  1583. X        }
  1584. X
  1585. X/* Unless this code is raw data, pursue the chain pointed to by CurCode
  1586. X * through the hash table to its end; each code in the chain puts its
  1587. X * associated output code on the output queue.
  1588. X */
  1589. X
  1590. X        while (CurCode > BitMask) {
  1591. X        OutCode[OutCount++] = Suffix[CurCode];
  1592. X        CurCode = Prefix[CurCode];
  1593. X        } 
  1594. X
  1595. X/* The last code in the chain is treated as raw data. */
  1596. X
  1597. X        FinChar = CurCode & BitMask;
  1598. X        OutCode[OutCount++] = FinChar;
  1599. X
  1600. X/* Now we put the data out to the Output routine.
  1601. X * It's been stacked LIFO, so deal with it that way...
  1602. X */
  1603. X
  1604. X        for (i = OutCount - 1; i >= 0; i--)
  1605. X        AddToPixel(OutCode[i]);
  1606. X        OutCount = 0;
  1607. X
  1608. X/* Build the hash table on-the-fly. No table is stored in the file. */
  1609. X
  1610. X        Prefix[FreeCode] = OldCode;
  1611. X        Suffix[FreeCode] = FinChar;
  1612. X        OldCode = InCode;
  1613. X
  1614. X/* Point to the next slot in the table.  If we exceed the current
  1615. X * MaxCode value, increment the code size unless it's already 12.  If it
  1616. X * is, do nothing: the next code decompressed better be CLEAR
  1617. X */
  1618. X
  1619. X        FreeCode++;
  1620. X        if (FreeCode >= MaxCode) {
  1621. X        if (CodeSize < 12) {
  1622. X            CodeSize++;
  1623. X            MaxCode *= 2;
  1624. X            ReadMask = (1 << CodeSize) - 1;
  1625. X        }
  1626. X        }
  1627. X    }
  1628. X    Code = ReadCode();
  1629. X    }
  1630. X
  1631. X    free(Raster);
  1632. X
  1633. X    fprintf(stderr, "done.\n");
  1634. X
  1635. X    if (!(fp = fopen(outfname, "w")))
  1636. X    error("%s: can't create %s.\n", outfname);
  1637. X
  1638. X#ifdef notdefSS
  1639. X/* SS: Pixrect Rasterfile output code. */
  1640. X    fprintf(stderr, "Writing Sun Rasterfile: %s...", outfname);
  1641. X    if (pr_dump(Output, fp, &Colormap, RT_STANDARD, 0) == PIX_ERR)
  1642. X    error("%s: error writing Sun Rasterfile: %s\n", outfname);
  1643. X#endif notdefSS
  1644. X/* PBMS: PBM output code. */
  1645. X    pbm_writepbm(stdout, bits, Width, Height);
  1646. X
  1647. X    fclose(fp);
  1648. X
  1649. X    fprintf(stderr, "done.\n");
  1650. X}
  1651. X
  1652. X
  1653. X/* Fetch the next code from the raster data stream.  The codes can be
  1654. X * any length from 3 to 12 bits, packed into 8-bit bytes, so we have to
  1655. X * maintain our location in the Raster array as a BIT Offset.  We compute
  1656. X * the byte Offset into the raster array by dividing this by 8, pick up
  1657. X * three bytes, compute the bit Offset into our 24-bit chunk, shift to
  1658. X * bring the desired code to the bottom, then mask it off and return it. 
  1659. X */
  1660. XReadCode()
  1661. X{
  1662. Xint RawCode, ByteOffset;
  1663. X
  1664. X    ByteOffset = BitOffset / 8;
  1665. X    RawCode = Raster[ByteOffset] + (0x100 * Raster[ByteOffset + 1]);
  1666. X    if (CodeSize >= 8)
  1667. X    RawCode += (0x10000 * Raster[ByteOffset + 2]);
  1668. X    RawCode >>= (BitOffset % 8);
  1669. X    BitOffset += CodeSize;
  1670. X    return(RawCode & ReadMask);
  1671. X}
  1672. X
  1673. X
  1674. XAddToPixel(Index)
  1675. Xu_char Index;
  1676. X{
  1677. X#ifdef notdefSS
  1678. X    *(Image + YC * Width + XC) = Index;
  1679. X#endif notdefSS
  1680. X/* PBMS: Store a pixel. */
  1681. X    bits[YC][XC] = Index;
  1682. X
  1683. X/* Update the X-coordinate, and if it overflows, update the Y-coordinate */
  1684. X
  1685. X    if (++XC == Width) {
  1686. X
  1687. X/* If a non-interlaced picture, just increment YC to the next scan line. 
  1688. X * If it's interlaced, deal with the interlace as described in the GIF
  1689. X * spec.  Put the decoded scan line out to the screen if we haven't gone
  1690. X * past the bottom of it
  1691. X */
  1692. X
  1693. X    XC = 0;
  1694. X    if (!Interlace) YC++;
  1695. X    else {
  1696. X        switch (Pass) {
  1697. X        case 0:
  1698. X            YC += 8;
  1699. X            if (YC >= Height) {
  1700. X            Pass++;
  1701. X            YC = 4;
  1702. X            }
  1703. X        break;
  1704. X        case 1:
  1705. X            YC += 8;
  1706. X            if (YC >= Height) {
  1707. X            Pass++;
  1708. X            YC = 2;
  1709. X            }
  1710. X        break;
  1711. X        case 2:
  1712. X            YC += 4;
  1713. X            if (YC >= Height) {
  1714. X            Pass++;
  1715. X            YC = 1;
  1716. X            }
  1717. X        break;
  1718. X        case 3:
  1719. X            YC += 2;
  1720. X        break;
  1721. X        default:
  1722. X        break;
  1723. X        }
  1724. X    }
  1725. X    }
  1726. X}
  1727. SHAR_EOF
  1728. if test 13188 -ne "`wc -c < 'giftopbm.c'`"
  1729. then
  1730.     echo shar: error transmitting "'giftopbm.c'" '(should have been 13188 characters)'
  1731. fi
  1732. fi # end of overwriting check
  1733. echo shar: extracting "'giftopbm.1'" '(952 characters)'
  1734. if test -f 'giftopbm.1'
  1735. then
  1736.     echo shar: will not over-write existing file "'giftopbm.1'"
  1737. else
  1738. sed 's/^X//' << \SHAR_EOF > 'giftopbm.1'
  1739. X.TH giftopbm 1 "29 August 1988"
  1740. X.SH NAME
  1741. Xgiftopbm - convert GIF files into portable bitmaps
  1742. X.SH SYNOPSIS
  1743. Xgiftopbm [giffile]
  1744. X.SH DESCRIPTION
  1745. XReads a GIF file as input.
  1746. XProduces a portable bitmap as output.
  1747. X.LP
  1748. XNote that there is currently no pbmtogif tool.
  1749. X.LP
  1750. XAlso note that, since PBM only supports monochrome, this tool is
  1751. Xuseless for 99% of the GIF files out there.
  1752. X.SH BUGS
  1753. XThis tool has not been tested in its PBM form AT ALL.
  1754. XHowever, in the original gif2ras form, it worked fine.
  1755. X.SH "SEE ALSO"
  1756. Xpbm(5)
  1757. X.SH AUTHOR
  1758. XTrivial modifications to produce PBM by Jef Poskanzer.  Otherwise:
  1759. X
  1760. XCopyright (c) 1988 by Patrick J. Naughton
  1761. X(naughton@wind.sun.com)
  1762. X
  1763. XPermission to use, copy, modify, and distribute this software and its
  1764. Xdocumentation for any purpose and without fee is hereby granted,
  1765. Xprovided that the above copyright notice appear in all copies and that
  1766. Xboth that copyright notice and this permission notice appear in
  1767. Xsupporting documentation. 
  1768. SHAR_EOF
  1769. if test 952 -ne "`wc -c < 'giftopbm.1'`"
  1770. then
  1771.     echo shar: error transmitting "'giftopbm.1'" '(should have been 952 characters)'
  1772. fi
  1773. fi # end of overwriting check
  1774. #    End of shell archive
  1775. exit 0
  1776.